package com.zbw.weightlibrary.WeightSub;

import com.qhscale.QHJNIScale;
import com.qhscale.data.WeightV2;
import com.zbw.weightlibrary.Connector.BaseControlImpl;
import com.zbw.weightlibrary.Connector.IinfoCallback;
import com.zbw.weightlibrary.Connector.WeightData;
import com.qhscale.data.QHADCallback;

/**
 * 作者 ：Wesley
 * 时间 ：2023-11-28 14:24
 * 这个类是干嘛的？：QiHuaSerialPort
 */
public class QiHuaSerialPort extends BaseControlImpl {
   private final IinfoCallback mCallback;
   private final String mPort; //串口电子称端口port
   private final int mBaudRate; //串口电子称波特率baudrate
   private QHJNIScale scale;
   private boolean stat = true;


   public QiHuaSerialPort(IinfoCallback callback, WeightData data) {
      super(callback);
      mCallback = callback;
      mPort = data.getPort();
      mBaudRate = data.getBaudrate();
   }

   @Override
   public void bleConnect() {
      scale = QHJNIScale.Companion.getScale(mPort);
      scale.setCallback(mQHADCallback);
      mCallback.onConnect("初始化成功", true);
   }

   private QHADCallback mQHADCallback = new QHADCallback() {
      @Override
      public void onWeightUpdate(WeightV2 weightV2, boolean isStable, boolean b1, boolean b2) {
         if(mCallback != null && stat){
            mCallback.onGetWeight(decimalFormat.format(weightV2.getWeight()),isStable);
         }
      }

      @Override
      public void onCalibrationSwitchEvent() {

      }
   };

   @Override
   public void bleDisconnect() {
      if (scale != null) {
         scale.deleteScale();
         scale.clear();
         scale = null;
      }
   }

   @Override
   public void getWeight() {
      stat = true;
   }

   @Override
   public void stopWeight() {
      stat = false;
   }

   @Override
   public void clearTare() {
      if(scale != null){
         scale.forceZero();
      }
   }

   @Override
   public void tare() {
      if(scale != null){
         scale.tare();
      }
   }

   @Override
   public void zero() {
      if(scale != null){
         scale.zero();
      }
   }
}
